<?php
/**前台公用控制器
 * @Author: 976123967@qq.com
 * @Date:   2014-09-03 22:14:15
 * @Last Modified by:   Administrator
 * @Last Modified time: 2014-12-18 11:10:09
 */
class CommonAction Extends ExtendAction
{
	// 栏目模型
	public $categoyModel;
	// 文档模型
	public $articleModel;
	// 文档视图模型
	public $articleViewModel;
	// 图片集模型
	public $picModel;
	// 栏目cid
	public $cid; 
	// 文档cid
	public $aid;

	// 初始化 
	public function _initialize()
	{
		/**************接收数据**************/
		// 当前访问栏目的cid
		$this->cid = I('get.cid',0,'intval');
		// 当前访问的文档aid
		$this->aid = I('get.aid',0,'intval');

		/*************验证*******************/
		// 以下方法不做cid是否存在验证
		$actionName = array('index','verify','map');
		if(!$this->cid && !in_array(ACTION_NAME, $actionName) )
			$this->redirect("index/index");

		/*************初始化模型对象**********/
		$this->categoryModel = D('Category');
		$this->articleModel = D('Article');
		$this->articleViewModel = D('ArticleView');
		$this->picModel = D('Pic');


		/*************验证系统主题*********/
		/*$theme = I('get.t',null);
		if($theme)
		{
			session('curTheme',$theme);
		}
		elseif(!isset($_SESSION['curTheme']))
		{
			session('curTheme',C('DEFAULT_THEME'));
		}
	
		C('DEFAULT_THEME',$_SESSION['curTheme']);*/


	}


	/**
	 * [cover 封面页]
	 * @return [type] [description]
	 */
	public function cover()
	{
		/**********栏目基础数据*****************/
		$cid = $this->cid;
		$cms= $this->base();


		/***********SEO优化数据****************/
		// 关键字
    	$cms['page_keywords'] = $cms['page_keywords']?$cms['page_keywords']:C('cfg_name');
    	//$cms['page_keywords_en'] = $cms['page_keywords_en']?$cms['page_keywords_en']:C('cfg_keywords_en');
    	// 描述
    	$cms['page_description'] = $cms['page_description']?$cms['page_description']:C('cfg_description');
    	//$cms['page_description_en'] = $cms['page_description_en']?$cms['page_description_en']:C('cfg_description_en');
    

		

		/**********模板数据*****************/
		// 给模板赋值
		$this->assign('cms',$cms);
		// 获取模板
		$defaultTpl = $this->categoryModel->get_field($cid,'default_tpl');
		$this->display($defaultTpl);

	}
	

	 /**
     * [lists 列表页]
     * @return [type] [description]
     */
    public function lists()
    {
    	/**********栏目基础数据*****************/
    	$cid = $this->cid;
		$cms= $this->base();


		/***********SEO优化数据****************/
		// 关键字
    	$cms['page_keywords'] = $cms['page_keywords']?$cms['page_keywords']:C('cfg_name');
    	//$cms['page_keywords_en'] = $cms['page_keywords_en']?$cms['page_keywords_en']:C('cfg_keywords_en');
    	// 描述
    	$cms['page_description'] = $cms['page_description']?$cms['page_description']:C('cfg_decription');
    	//$cms['page_description_en'] = $cms['page_description_en']?$cms['page_description_en']:C('cfg_description_en');
    	
    

		/**********模板数据*****************/
		// 给模板赋值
		$this->assign('cms',$cms);
		// 获取模板
		$listTpl = $this->categoryModel->get_field($cid,'list_tpl');
		$this->display($listTpl);

    }


   	/**
   	 * [view 详细页面]
   	 * @return [type] [description]
   	 */
    public function view()
    {
    	$cid = $this->cid;
    	$aid = $this->aid;


    	/************单页面获取aid***************/
		// $aid为0代表是单页面
		if(!$aid)
			$aid = $this->articleModel->get_article_cid($cid);
		

		/*************验证**************/
		// 必须有aid
		if(!$aid)
			$this->redirect('Index/index');
		// 获取数据
		$data = $this->articleViewModel->get_article_one($aid);

		// 必须有数据
		// 没有信息
		if(!$data)
			$this->redirect('Index/index');


		// 栏目基础数据
		$cms=$this->base();
		// 合并数据
		$cms = array_merge($data,$cms);
		// 格式化时间数据
		$cms['time'] = $data['addtime'];
		$cms['addtime'] = date('Y-m-d',$data['addtime']);

		/***************SEO优化数据*********/
		// 重置seo优化数据
		$cms['page_keywords'] = $data['keywords'];
		//$cms['page_keywords_en'] = $data['keywords_en'];
		$cms['page_description'] = $data['description'];
		//$cms['page_description_en'] = $data['description_en'];

		
		// 页面标题
		if($cms['cat_type']!=4)
		{
			$cms['page_title'] = $cms['article_title'].' > '.$cms['page_title'];
			//$cms['page_title_en'] = $cms['article_title_en'].' > '.$cms['page_title_en'];
		}
	
		// 关键字
    	$cms['page_keywords'] = $cms['page_keywords']?$cms['page_keywords']:C('cfg_keywords');
    	//$cms['page_keywords_en'] = $cms['page_keywords_en']?$cms['page_keywords_en']:C('cfg_keywords_en');
    	// 描述
    	$cms['page_description'] = $cms['page_description']?$cms['page_description']:C('cfg_description');
    	//$cms['page_description_en'] = $cms['page_description_en']?$cms['page_description_en']:C('cfg_description_en');

    	
		// 点击次数
		$this->articleModel->update_click($data['aid']);
	
        // 所有图集
        $pics = $this->picModel->get_all($aid);
        

		/**********模板数据*****************/
		// 给模板赋值
		$this->assign('cms',$cms);
		$this->assign('pics',$pics);
		// 获取模板
		$listTpl = $this->categoryModel->get_field($cid,'view_tpl');
		$this->display($listTpl);
    }


    /**
     * [base 栏目基本信息]
     * @return [type]      [description]
     */
    public function base()
    {
    	$cid = $this->cid;

    	/************当前访问栏目数据******************/

    	// 栏目所有数据 缓存中取得
    	$cateInfo = $this->categoryModel->find_one($cid);
    
		// 栏目类型 普通 单页 跳转
		$data['cat_type'] = $cateInfo['cat_type'];


		// 当前访问的栏目名称
		$data['cname']= $cateInfo['cname'];
		//$data['cname_en']= $cateInfo['cname_en'];

		// 当前栏目的图片
		$categoryPic = $cateInfo['pic'];
		$data['category_pic'] = $categoryPic?__ROOT__.'/'.$categoryPic:'';

		/**************顶级栏目数据******************/
		// 顶级栏目cid
		$parentCids = $this->categoryModel->get_parent_category_cid($cid);
		$parentCids = array_reverse($parentCids);
		$data['topcid']= $parentCids[0];
		// 顶栏目所有数据 缓存中取得
		$topCateInfo = $this->categoryModel->find_one($parentCids[0]);
		//p($topCateInfo);die;

		// 顶级栏目名称
		$data['topname'] = $topCateInfo['cname'];
		$data['topname_en']= $topCateInfo['cname_en'];
		

		/***************面包屑导航********************/ 
		$bread = $this->categoryModel->get_bread($cid,$this->aid);
		$data['bread']= $bread['bread'];
		//$data['bread_en']= $bread['bread_en'];
		

		

		/**************SEO优化数据*******************/
		// 标题
		$pagTitle = $this->categoryModel->get_title($cid);
		$data['page_title'] = $pagTitle['title'];
		//$data['page_title_en'] = $pagTitle['title_en'];
	
		// 关键字
		$data['page_keywords'] = $cateInfo['keywords'];
		//$data['page_keywords_en'] = $cateInfo['keywords_en'];
		// 描述
		$data['page_description'] = $cateInfo['description'];
		//$data['page_description_en'] = $cateInfo['description_en'];

		
		return $data;
    }


   

}